﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microhard.Protal.Models;
using System.Web.Mvc;
using Microhard.Protal.Interfaces;
using Microhard.Core;
using Microhard.Permission.Models;
using Microhard.Permission.Interface;


namespace Microhard.WebUI.Protal.Extensions
{
    public static class HtmlExtensions
    {
        public static AttachmentModel GetFile(this HtmlHelper helper, string type, Guid mainDataID)
        {
            IFile file = IoC.Resolve<IFile>();
            var f = file.GetModelByModel(new AttachmentModel
            {
                MainDataID = mainDataID,
                FileType = type
            });
            return f;
        }

        public static List<ArticleModel> GetArticleByDicName(this HtmlHelper helper, string dicName, int top)
        {
            List<ArticleModel> arts = new List<ArticleModel>();

            IDictionary dic = IoC.Resolve<IDictionary>();
            var dics = dic.Query(new DictionaryModel { Name = dicName });
            if (dics.Count > 0)
            {
                var fdic = dics.First();
                IArticle art = IoC.Resolve<IArticle>();
                arts = art.Query(new ArticleModel { ArticleType = fdic.Value });
                if (top > 0)
                {
                    arts = arts.OrderByDescending(c => c.CreateTime).Take(top).ToList();
                }
            }
            return arts;
        }

        public static List<ArticleModel> GetTopArticle(this HtmlHelper helper, ArticleModel model, int top)
        {
            IArticle cat = IoC.Resolve<IArticle>();
            var data = cat.GetTopArticles(model, top);
            return data;
        }

        public static List<ProductModel> GetTopProduct(this HtmlHelper helper, int top)
        {
            IProduct cat = IoC.Resolve<IProduct>();
            var data = cat.GetLatestTopProducts(top);
            return data;
        }


        public static List<DictionaryModel> GetDics(this HtmlHelper helper, string type)
        {
            IDictionary cat = IoC.Resolve<IDictionary>();
            var data = cat.Query(new DictionaryModel { Type = type });
            return data;
        }

        public static List<CategoryModel> GetCategoryRootWithoutNone(this HtmlHelper helper, Guid companyID)
        {
            ICategory cat = IoC.Resolve<ICategory>();
            var data = cat.GetRootCategories(null, companyID);
            return data;
        }


        /// <summary>
        /// 门户类型选择
        /// </summary>
        /// <param name="helper"></param>
        /// <returns></returns>
        public static List<CategoryModel> GetCategoryRoot(this HtmlHelper helper, Guid companyID)
        {
            ICategory cat = IoC.Resolve<ICategory>();
            var data = cat.GetRootCategories(null, companyID);
            data.Insert(0, new CategoryModel { Name = "无", CategoryID = Guid.Empty });
            return data;
        }

        /// <summary>
        /// 门户类型菜单
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static CategoryModel GetSingleCategoryMenu(this HtmlHelper helper, string name, Guid companyID)
        {
            ICategory cat = IoC.Resolve<ICategory>();
            var first = cat.GetRootCategories(name, companyID);
            if (first.Count > 0)
                return first.First();
            return new CategoryModel();
        }

        /// <summary>
        /// 获得所有二级菜单
        /// </summary>
        /// <param name="helper"></param>
        /// <returns></returns>
        public static List<CategoryModel> GetSecondSubCategoryMenus(this HtmlHelper helper, Guid companyID)
        {
            ICategory cat = IoC.Resolve<ICategory>();
            var data = cat.GetRootCategories(null, companyID);
            List<CategoryModel> result = new List<CategoryModel>();
            data.ForEach(item =>
            {
                if (item.Subs.Count > 0)
                {
                    result.AddRange(item.Subs);
                }
            });
            return result;
        }

        public static SiteInfoModel GetSiteInfo(this HtmlHelper helper, string keyName)
        {
            ISiteInfo isi = IoC.Resolve<ISiteInfo>();
            return isi.GetModelByModel(new SiteInfoModel { Key = keyName });
        }
    }
}